package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import fileIO.Student;

public class ClassTest {
	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException, Exception,
			InvocationTargetException {
//		methodInvoketest();
//		classInstanceTest();
		newInstancetest();
	}

	private static void methodInvoketest() throws SecurityException,
			NoSuchMethodException, Exception, IllegalAccessException,
			InvocationTargetException {
		Student stu = new Student("", "", 0);
		methodInvoke(stu, "name", "mark");
		methodInvoke(stu, "gender", "nan");
		System.out.println(stu);
		Date d = new Date();
		methodInvoke(d, "time", 0L);
		System.out.println(d);
		System.out.println(d.getTime());
	}

	private static void methodInvoke(Object obj, String attr, Object value)
			throws SecurityException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException {
		Class c = obj.getClass();
		String name = "set" + attr.substring(0, 1).toUpperCase()
				+ attr.substring(1);
		Method m = null;
		try {
			m = c.getMethod(name, new Class[] { value.getClass() });
		} catch (Exception e) {
			if (value.getClass() == Integer.class) {
				m = c.getMethod(name, new Class[] { int.class });
			} else if (value.getClass() == Long.class) {
				m = c.getMethod(name, new Class[] { long.class });
			}
		}
		// obj.setter(value);
		Object returnValue = m.invoke(obj, new Object[] { value });
	}

	private static void accessibleTest() throws SecurityException,
			NoSuchFieldException, Exception, IllegalAccessException {
		Student stu = new Student("xiaoming", "nan", 20);
		Class c = stu.getClass();
		Field f = c.getDeclaredField("name");
		f.setAccessible(true);//璁块棶绉佹湁灞炴�
		f.set(stu, "lisi");
		Object value = f.get(stu);
		System.out.println(value);
	}

	private static void newInstancetest() throws Exception, NoSuchMethodException {
		Class c=Student.class;
		Constructor cc=c.getDeclaredConstructor();
		Object o=cc.newInstance();
		System.out.println(o);
		
		
		Class ccc=DefaultPhoneNotifier.class;
		Constructor cNotifier=ccc.getDeclaredConstructor();
		
//		Class c = Student.class;
//		Constructor co = c.getConstructor(new Class[] { String.class,
//				String.class, int.class });
//		Object obj = co.newInstance(new Object[] { "jili", "wangbadai", 20 });
//		System.out.println(obj);
//		Class c=Class.forName("fileIO.Student");
		
		
//		Constructor c=Class.forName("fileIO.Student").getDeclaredConstructor(new Class[]{String.class,String.class,int.class});
//		c.setAccessible(true);
//		Object o=c.newInstance(new Object[]{"jili","wangbadai",20});
		
		
//		o.getClass()
//		fileIO.Student t=(fileIO.Student)o;
//		System.out.println(o);
	}

	private static void getConstructorMethodField(Object obj) {
		Class o = obj.getClass();
		System.out.println(o);
		System.out.println("method************************************");
		Method[] m = o.getMethods();
		for (Method method : m) {
			System.out.println(method);
		}
		System.out.println("constructor************************************");
		Constructor[] c = o.getConstructors();
		for (Constructor constructor : c) {
			System.out.println(constructor);
		}
		System.out.println("fileds************************************");

		Field[] f = o.getFields();
		for (Field field : f) {
			System.out.println(field);
		}
	}

	private static void classInstanceTest() throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		Date d = new Date();
		Class c1 = d.getClass();
		Class c2 = Date.class;
		c2.newInstance();
		Class c3 = Class.forName("java.util.Date");
		System.out.println(c1 == c2);
		System.out.println(c1 == c3);
		System.out.println(c2 == c3);
		System.out.println(c2);
		Date d1 = (Date) c3.newInstance();
		System.out.println(d1);
		Constructor[] d2 = c3.getConstructors();
		System.out.println("******************");
		for (Constructor c : d2) {
			System.out.println(c.newInstance());
		}
	}
}
